home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-10-31 | 55.5 KB | 1,643 lines |
- Newsgroups: comp.sources.misc
- From: daveg@synaptics.com (David Gillespie)
- Subject: v24i081: gnucalc - GNU Emacs Calculator, v2.00, Part33/56
- Message-ID: <1991Oct31.214457.2376@sparky.imd.sterling.com>
- X-Md4-Signature: 05542d57c8e0883ddc40c41b97f7c944
- Date: Thu, 31 Oct 1991 21:44:57 GMT
- Approved: kent@sparky.imd.sterling.com
-
- Submitted-by: daveg@synaptics.com (David Gillespie)
- Posting-number: Volume 24, Issue 81
- Archive-name: gnucalc/part33
- Environment: Emacs
- Supersedes: gmcalc: Volume 13, Issue 27-45
-
- ---- Cut Here and unpack ----
- #!/bin/sh
- # do not concatenate these parts, unpack them in order with /bin/sh
- # file calc.texinfo continued
- #
- if test ! -r _shar_seq_.tmp; then
- echo 'Please unpack part 1 first!'
- exit 1
- fi
- (read Scheck
- if test "$Scheck" != 33; then
- echo Please unpack part "$Scheck" next!
- exit 1
- else
- exit 0
- fi
- ) < _shar_seq_.tmp || exit 1
- if test ! -f _shar_wnt_.tmp; then
- echo 'x - still skipping calc.texinfo'
- else
- echo 'x - continuing file calc.texinfo'
- sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
- vector.
- X
- (@bullet{}) @strong{Exercise 1.} Use @samp{*} to sum along the rows
- of the above @c{$2\times3$}
- @asis{2x3} matrix to get @cite{[6, 15]}. Now use @samp{*} to
- sum along the columns to get @cite{[5, 7, 9]}.
- @xref{Matrix Answer 1, 1}. (@bullet{})
- X
- @cindex Identity matrix
- An @dfn{identity matrix} is a square matrix with ones along the
- diagonal and zeros elsewhere. It has the property that multiplication
- by an identity matrix, on the left or on the right, always produces
- the original matrix.
- X
- @group
- @smallexample
- 1: [ [ 1, 2, 3 ] 2: [ [ 1, 2, 3 ] 1: [ [ 1, 2, 3 ]
- X [ 4, 5, 6 ] ] [ 4, 5, 6 ] ] [ 4, 5, 6 ] ]
- X . 1: [ [ 1, 0, 0 ] .
- X [ 0, 1, 0 ]
- X [ 0, 0, 1 ] ]
- X .
- X
- X r 4 v i 3 RET *
- @end smallexample
- @end group
- X
- If a matrix is square, it is often possible to find its @dfn{inverse},
- that is, a matrix which, when multiplied by the original matrix, yields
- an identity matrix. The @kbd{&} (reciprocal) key also computes the
- inverse of a matrix.
- X
- @group
- @smallexample
- 1: [ [ 1, 2, 3 ] 1: [ [ -2.4, 1.2, -0.2 ]
- X [ 4, 5, 6 ] [ 2.8, -1.4, 0.4 ]
- X [ 7, 6, 0 ] ] [ -0.73333, 0.53333, -0.2 ] ]
- X . .
- X
- X r 4 r 2 | s 5 &
- @end smallexample
- @end group
- X
- @noindent
- The vertical bar @kbd{|} @dfn{concatenates} numbers, vectors, and
- matrices together. Here we have used it to add a new row onto
- our matrix to make it square.
- X
- We can multiply these two matrices in either order to get an identity.
- X
- @group
- @smallexample
- 1: [ [ 1., 0., 0. ] 1: [ [ 1., 0., 0. ]
- X [ 0., 1., 0. ] [ 0., 1., 0. ]
- X [ 0., 0., 1. ] ] [ 0., 0., 1. ] ]
- X . .
- X
- X M-RET * U TAB *
- @end smallexample
- @end group
- X
- @cindex Systems of linear equations
- @cindex Linear equations, systems of
- Matrix inverses are related to systems of linear equations in algebra.
- Suppose we had the following set of equations:
- X
- @ifinfo
- @group
- @example
- X a + 2b + 3c = 6
- X 4a + 5b + 6c = 2
- X 7a + 6b = 3
- @end example
- @end group
- @end ifinfo
- @tex
- \turnoffactive
- $$ \openup1\jot \tabskip=0pt plus1fil
- \halign to\displaywidth{\tabskip=0pt
- X $\hfil#$&$\hfil{}#{}$&
- X $\hfil#$&$\hfil{}#{}$&
- X $\hfil#$&${}#\hfil$\tabskip=0pt plus1fil\cr
- X a&+&2b&+&3c&=6 \cr
- X 4a&+&5b&+&6c&=2 \cr
- X 7a&+&6b& & &=3 \cr}
- $$
- @end tex
- X
- @noindent
- This can be cast into the matrix equation,
- X
- @ifinfo
- @group
- @example
- X [ [ 1, 2, 3 ] [ [ a ] [ [ 6 ]
- X [ 4, 5, 6 ] * [ b ] = [ 2 ]
- X [ 7, 6, 0 ] ] [ c ] ] [ 3 ] ]
- @end example
- @end group
- @end ifinfo
- @tex
- \turnoffactive
- $$ \pmatrix{ 1 & 2 & 3 \cr 4 & 5 & 6 \cr 7 & 6 & 0 }
- X \times
- X \pmatrix{ a \cr b \cr c } = \pmatrix{ 6 \cr 2 \cr 3 }
- $$
- @end tex
- X
- We can solve this system of equations by multiplying both sides by the
- inverse of the matrix. Calc can do this all in one step:
- X
- @group
- @smallexample
- 2: [6, 2, 3] 1: [-12.6, 15.2, -3.93333]
- 1: [ [ 1, 2, 3 ] .
- X [ 4, 5, 6 ]
- X [ 7, 6, 0 ] ]
- X .
- X
- X [6,2,3] r 5 /
- @end smallexample
- @end group
- X
- @noindent
- The result is the @cite{[a, b, c]} vector that solves the equations.
- (Dividing by a square matrix is equivalent to multiplying by its
- inverse.)
- X
- Let's verify this solution:
- X
- @group
- @smallexample
- 2: [ [ 1, 2, 3 ] 1: [6., 2., 3.]
- X [ 4, 5, 6 ] .
- X [ 7, 6, 0 ] ]
- 1: [-12.6, 15.2, -3.93333]
- X .
- X
- X r 5 TAB *
- @end smallexample
- @end group
- X
- @noindent
- Note that we had to be careful about the order in which we multiplied
- the matrix and vector. If we multiplied in the other order, Calc would
- assume the vector was a row vector in order to make the dimensions
- come out right, and the answer would be incorrect. If you
- don't feel safe letting Calc take either interpretation of your
- vectors, use explicit @c{$N\times1$}
- @asis{Nx1} or @c{$1\times N$}
- @asis{1xN} matrices instead.
- In this case, you would enter the original column vector as
- @samp{[[6], [2], [3]]} or @samp{[6; 2; 3]}.
- X
- (@bullet{}) @strong{Exercise 2.} Algebraic entry allows you to make
- vectors and matrices that include variables. Solve the following
- system of equations to get expressions for @cite{x} and @cite{y}
- in terms of @cite{a} and @cite{b}.
- X
- @ifinfo
- @group
- @example
- X x + a y = 6
- X x + b y = 10
- @end example
- @end group
- @end ifinfo
- @tex
- \turnoffactive
- $$ \eqalign{ x &+ a y = 6 \cr
- X x &+ b y = 10}
- $$
- @end tex
- X
- @noindent
- @xref{Matrix Answer 2, 2}. (@bullet{})
- X
- @cindex Least-squares for over-determined systems
- @cindex Over-determined systems of equations
- (@bullet{}) @strong{Exercise 3.} A system of equations is ``over-determined''
- if it has more equations than variables. It is often the case that
- there are no values for the variables that will satisfy all the
- equations at once, but it is still useful to find a set of values
- which ``nearly'' satisfy all the equations. In terms of matrix equations,
- you can't solve @cite{A X = B} directly because the matrix @cite{A}
- is not square for an over-determined system. Matrix inversion works
- only for square matrices. One common trick is to multiply both sides
- on the left by the transpose of @cite{A}:
- @ifinfo
- @samp{trn(A)*A*X = trn(A)*B}.
- @end ifinfo
- @tex
- \turnoffactive
- $A^T A \, X = A^T B$, where $A^T$ is the transpose \samp{trn(A)}.
- @end tex
- Now @c{$A^T A$}
- @cite{trn(A)*A} is a square matrix so a solution is possible. It
- turns out that the @cite{X} vector you compute in this way will be a
- ``least-squares'' solution, which can be regarded as the ``closest''
- solution to the set of equations. Use Calc to solve the following
- over-determined system:@refill
- X
- @ifinfo
- @group
- @example
- X a + 2b + 3c = 6
- X 4a + 5b + 6c = 2
- X 7a + 6b = 3
- X 2a + 4b + 6c = 11
- @end example
- @end group
- @end ifinfo
- @tex
- \turnoffactive
- $$ \openup1\jot \tabskip=0pt plus1fil
- \halign to\displaywidth{\tabskip=0pt
- X $\hfil#$&$\hfil{}#{}$&
- X $\hfil#$&$\hfil{}#{}$&
- X $\hfil#$&${}#\hfil$\tabskip=0pt plus1fil\cr
- X a&+&2b&+&3c&=6 \cr
- X 4a&+&5b&+&6c&=2 \cr
- X 7a&+&6b& & &=3 \cr
- X 2a&+&4b&+&6c&=11 \cr}
- $$
- @end tex
- X
- @noindent
- @xref{Matrix Answer 3, 3}. (@bullet{})
- X
- @node List Tutorial, , Matrix Tutorial, Vector/Matrix Tutorial
- @subsection Vectors as Lists
- X
- @noindent
- @cindex Lists
- Although Calc has a number of features for manipulating vectors and
- matrices as mathematical objects, you can also treat vectors as
- simple lists of values. For example, we saw that the @kbd{k f}
- command returns a vector which is a list of the prime factors of a
- number.
- X
- You can pack and unpack stack entries into vectors:
- X
- @group
- @smallexample
- 3: 10 1: [10, 20, 30] 3: 10
- 2: 20 . 2: 20
- 1: 30 1: 30
- X . .
- X
- X M-3 v p v u
- @end smallexample
- @end group
- X
- You can also build vectors out of consecutive integers, or out
- of many copies of a given value:
- X
- @group
- @smallexample
- 1: [1, 2, 3, 4] 2: [1, 2, 3, 4] 2: [1, 2, 3, 4]
- X . 1: 17 1: [17, 17, 17, 17]
- X . .
- X
- X v x 4 RET 17 v b 4 RET
- @end smallexample
- @end group
- X
- You can apply an operator to every element of a vector using the
- @dfn{map} command.
- X
- @group
- @smallexample
- 1: [17, 34, 51, 68] 1: [289, 1156, 2601, 4624] 1: [17, 34, 51, 68]
- X . . .
- X
- X V M * 2 V M ^ V M Q
- @end smallexample
- @end group
- X
- @noindent
- In the first step, we multiply the vector of integers by the vector
- of 17's elementwise. In the second step, we raise each element to
- the power two. (The general rule is that both operands must be
- vectors of the same length, or else one must be a vector and the
- other a plain number.) In the final step, we take the square root
- of each element.
- X
- (@bullet{}) @strong{Exercise 1.} Compute a vector of powers of two
- from @c{$2^{-4}$}
- @cite{2^-4} to @cite{2^4}. @xref{List Answer 1, 1}. (@bullet{})
- X
- You can also @dfn{reduce} a binary operator across a vector.
- For example, reducing @samp{*} computes the product of all the
- elements in the vector:
- X
- @group
- @smallexample
- 1: 123123 1: [3, 7, 11, 13, 41] 1: 123123
- X . . .
- X
- X 123123 k f V R *
- @end smallexample
- @end group
- X
- @noindent
- In this example, we decompose 123123 into its prime factors, then
- multiply those factors together again to yield the original number.
- X
- We could compute a dot product ``by hand'' using mapping and
- reduction:
- X
- @group
- @smallexample
- 2: [1, 2, 3] 1: [7, 12, 0] 1: 19
- 1: [7, 6, 0] . .
- X .
- X
- X r 1 r 2 V M * V R +
- @end smallexample
- @end group
- X
- @noindent
- Recalling two vectors from the previous section, we compute the
- sum of pairwise products of the elements to get the same answer
- for the dot product as before.
- X
- A slight variant of vector reduction is the @dfn{accumulate} operation,
- @kbd{V U}. This produces a vector of the intermediate results from
- a corresponding reduction. Here we compute a table of factorials:
- X
- @group
- @smallexample
- 1: [1, 2, 3, 4, 5, 6] 1: [1, 2, 6, 24, 120, 720]
- X . .
- X
- X v x 6 RET V U *
- @end smallexample
- @end group
- X
- Calc allows vectors to grow as large as you like, although it gets
- rather slow if vectors have more than about a hundred elements.
- Actually, most of the time is spent formatting these large vectors
- for display, not calculating on them. Try the following experiment
- (if your computer is very fast you may need to substitute a larger
- vector size).
- X
- @group
- @smallexample
- 1: [1, 2, 3, 4, ... 1: [2, 3, 4, 5, ...
- X . .
- X
- X v x 500 RET 1 V M +
- @end smallexample
- @end group
- X
- Now press @kbd{v .} (the letter @kbd{v}, then a period) and try the
- experiment again. In @kbd{v .} mode, long vectors are displayed
- ``abbreviated'' like this:
- X
- @group
- @smallexample
- 1: [1, 2, 3, ..., 500] 1: [2, 3, 4, ..., 501]
- X . .
- X
- X v x 500 RET 1 V M +
- @end smallexample
- @end group
- X
- @noindent
- (where now the @samp{...} is actually part of the Calc display).
- You will find both operations are now much faster. But notice that
- even in @w{@kbd{v .}} mode, the full vectors are still shown in the Trail.
- Type @w{@kbd{t .}} to cause the trail to abbreviate as well, and try the
- experiment one more time. Operations on long vectors are now quite
- fast! (But of course if you use @kbd{t .} you will lose the ability
- to get old vectors back using the @kbd{t y} command.)
- X
- An easy way to view a full vector when @kbd{v .} mode is active is
- to press @kbd{`} (back-quote) to edit the vector; editing always works
- with the full, unabbreviated value.
- X
- @cindex Least-squares for fitting a straight line
- @cindex Fitting data to a line
- @cindex Line, fitting data to
- @cindex Data, extracting from buffers
- @cindex Columns of data, extracting
- As a larger example, let's try to fit a straight line to some data,
- using the method of least squares. (Calc has a built-in command for
- least-squares curve fitting, but we'll do it by hand here just to
- practice working with vectors.) Suppose we have the following list
- of values in a file we have loaded into Emacs:
- X
- @smallexample
- X x y
- X --- ---
- X 1.34 0.234
- X 1.41 0.298
- X 1.49 0.402
- X 1.56 0.412
- X 1.64 0.466
- X 1.73 0.473
- X 1.82 0.601
- X 1.91 0.519
- X 2.01 0.603
- X 2.11 0.637
- X 2.22 0.645
- X 2.33 0.705
- X 2.45 0.917
- X 2.58 1.009
- X 2.71 0.971
- X 2.85 1.062
- X 3.00 1.148
- X 3.15 1.157
- X 3.32 1.354
- @end smallexample
- X
- @noindent
- If you are reading this tutorial in printed form, you will find it
- easiest to press @kbd{M-# i} to enter the on-line Info version of
- the manual and find this table there. (Press @kbd{g}, then type
- @kbd{List Tutorial}, to jump straight to this section.)
- X
- Position the cursor at the upper-left corner of this table, just
- to the left of the @cite{1.34}. Press @kbd{C-@@} to set the mark.
- (On your system this may be @kbd{C-2}, @kbd{C-SPC}, or @kbd{NUL}.)
- Now position the cursor to the lower-right, just after the @cite{1.354}.
- You have now defined this region as an Emacs ``rectangle.'' Still
- in the Info buffer, type @kbd{M-# r}. This command
- (@code{calc-grab-rectangle}) will pop you back into the Calculator, with
- the contents of the rectangle you specified in the form of a matrix.@refill
- X
- @group
- @smallexample
- 1: [ [ 1.34, 0.234 ]
- X [ 1.41, 0.298 ]
- X @dots{}
- @end smallexample
- @end group
- X
- @noindent
- (You may wish to use @kbd{v .} mode to abbreviate the display of this
- large matrix.)
- X
- We want to treat this as a pair of lists. The first step is to
- transpose this matrix into a pair of rows. Remember, a matrix is
- just a vector of vectors. So we can unpack the matrix into a pair
- of row vectors on the stack.
- X
- @group
- @smallexample
- 1: [ [ 1.34, 1.41, 1.49, ... ] 2: [1.34, 1.41, 1.49, ... ]
- X [ 0.234, 0.298, 0.402, ... ] ] 1: [0.234, 0.298, 0.402, ... ]
- X . .
- X
- X v t v u
- @end smallexample
- @end group
- X
- @noindent
- Let's store these in quick variables 1 and 2, respectively.
- X
- @group
- @smallexample
- 1: [1.34, 1.41, 1.49, ... ] .
- X .
- X
- X t 2 t 1
- @end smallexample
- @end group
- X
- @noindent
- (Recall that @kbd{t 2} is a variant of @kbd{s 2} that removes the
- stored value from the stack.)
- X
- In a least squares fit, the slope @cite{m} is given by the formula
- X
- @ifinfo
- @example
- m = (N sum(x y) - sum(x) sum(y)) / (N sum(x^2) - sum(x)^2)
- @end example
- @end ifinfo
- @tex
- \turnoffactive
- $$ m = {N \sum x y - \sum x \sum y \over
- X N \sum x^2 - \left( \sum x \right)^2} $$
- @end tex
- X
- @noindent
- where @c{$\sum x$}
- @cite{sum(x)} represents the sum of all the values of @cite{x}.
- While there is an actual @code{sum} function in Calc, it's easier to
- sum a vector using a simple reduction. First, let's compute the four
- different sums that this formula uses.
- X
- @group
- @smallexample
- 1: 41.63 1: 98.0003
- X . .
- X
- X r 1 V R + t 3 r 1 2 V M ^ V R + t 4
- X
- @end smallexample
- @end group
- @noindent
- @group
- @smallexample
- 1: 13.613 1: 33.36554
- X . .
- X
- X r 2 V R + t 5 r 1 r 2 V M * V R + t 6
- @end smallexample
- @end group
- X
- @ifinfo
- @noindent
- These are @samp{sum(x)}, @samp{sum(x^2)}, @samp{sum(y)}, and @samp{sum(x y)},
- respectively. (We could have used @kbd{*} to compute @samp{sum(x^2)} and
- @samp{sum(x y)}.)
- @end ifinfo
- @tex
- \turnoffactive
- These are $\sum x$, $\sum x^2$, $\sum y$, and $\sum x y$,
- respectively. (We could have used \kbd{*} to compute $\sum x^2$ and
- $\sum x y$.)
- @end tex
- X
- Finally, we also need @cite{N}, the number of data points. This is just
- the length of either of our lists.
- X
- @group
- @smallexample
- 1: 19
- X .
- X
- X r 1 v l t 7
- @end smallexample
- @end group
- X
- @noindent
- (That's @kbd{v} followed by a lower-case @kbd{l}.)
- X
- Now we grind through the formula:
- X
- @group
- @smallexample
- 1: 633.94526 2: 633.94526 1: 67.23607
- X . 1: 566.70919 .
- X .
- X
- X r 7 r 6 * r 3 r 5 * -
- X
- @end smallexample
- @end group
- @noindent
- @group
- @smallexample
- 2: 67.23607 3: 67.23607 2: 67.23607 1: 0.52141679
- 1: 1862.0057 2: 1862.0057 1: 128.9488 .
- X . 1: 1733.0569 .
- X .
- X
- X r 7 r 4 * r 3 2 ^ - / t 8
- @end smallexample
- @end group
- X
- That gives us the slope @cite{m}. The y-intercept @cite{b} can now
- be found with the simple formula,
- X
- @ifinfo
- @example
- b = (sum(y) - m sum(x)) / N
- @end example
- @end ifinfo
- @tex
- \turnoffactive
- $$ b = {\sum y - m \sum x \over N} $$
- @end tex
- X
- @group
- @smallexample
- 1: 13.613 2: 13.613 1: -8.09358 1: -0.425978
- X . 1: 21.70658 . .
- X .
- X
- X r 5 r 8 r 3 * - r 7 / t 9
- @end smallexample
- @end group
- X
- Let's ``plot'' this straight line approximation, @c{$y \approx m x + b$}
- @cite{m x + b}, and compare it with the original data.@refill
- X
- @group
- @smallexample
- 1: [0.699, 0.735, ... ] 1: [0.273, 0.309, ... ]
- X . .
- X
- X r 1 r 8 * r 9 + s 0
- @end smallexample
- @end group
- X
- @noindent
- Notice that multiplying a vector by a constant, and adding a constant
- to a vector, can be done without mapping commands since these are
- common operations from vector algebra. As far as Calc is concerned,
- we've just been doing geometry in 19-dimensional space!
- X
- We can subtract this vector from our original @cite{y} vector to get
- a feel for the error of our fit. Let's find the maximum error:
- X
- @group
- @smallexample
- 1: [0.0387, 0.0112, ... ] 1: [0.0387, 0.0112, ... ] 1: 0.0897
- X . . .
- X
- X r 2 - V M A V R X
- @end smallexample
- @end group
- X
- @noindent
- First we compute a vector of differences, then we take the absolute
- values of these differences, then we reduce the @code{max} function
- across the vector. (The @code{max} function is on the two-key sequence
- @kbd{f x}; because it is so common to use @code{max} in a vector
- operation, the letters @kbd{X} and @kbd{N} are also accepted for
- @code{max} and @code{min} in this context. In general, you answer
- the @kbd{V M} or @kbd{V R} prompt with the actual key sequence that
- invokes the function you want. You could have typed @kbd{V R f x} or
- even @kbd{V R x max @key{RET}} if you had preferred.)
- X
- If your system has the GNUPLOT program, you can see graphs of your
- data and your straight line to see how well they match. (If you have
- GNUPLOT 3.0, the following instructions will work regardless of the
- kind of display you have. Some GNUPLOT 2.0, non-X-windows systems
- may require additional steps to view the graphs.)
- X
- Let's start by plotting the original data. Recall the ``@i{x}'' and ``@i{y}''
- vectors onto the stack and press @kbd{g f}. This ``fast'' graphing
- command does everything you need to do for simple, straightforward
- plotting of data.
- X
- @group
- @smallexample
- 2: [1.34, 1.41, 1.49, ... ]
- 1: [0.234, 0.298, 0.402, ... ]
- X .
- X
- X r 1 r 2 g f
- @end smallexample
- @end group
- X
- If all goes well, you will shortly get a new window containing a graph
- of the data. (If not, contact your GNUPLOT or Calc installer to find
- out what went wrong.) In the X window system, this will be a separate
- graphics window. In other kinds of displays, the default is to
- display the graphics in Emacs itself using rough character graphics.
- Press @kbd{q} when you are done viewing the character graphics.
- X
- Next, let's add the line we got from our least-squares fit:
- X
- @group
- @smallexample
- 2: [1.34, 1.41, 1.49, ... ]
- 1: [0.273, 0.309, 0.351, ... ]
- X .
- X
- X DEL r 0 g a g p
- @end smallexample
- @end group
- X
- It's not very useful to get symbols to mark the data points on this
- second curve; you can type @kbd{g S g p} to remove them. Type @kbd{g q}
- when you are done to remove the X graphics window and terminate GNUPLOT.
- X
- (@bullet{}) @strong{Exercise 2.} An earlier exercise showed how to do
- least squares fitting to a general system of equations. Our 19 data
- points are really 19 equations of the form @cite{y_i = m x_i + b} for
- different pairs of @cite{(x_i,y_i)}. Use the matrix-transpose method
- to solve for @cite{m} and @cite{b}, duplicating the above result.
- @xref{List Answer 2, 2}. (@bullet{})
- X
- @cindex Geometric mean
- (@bullet{}) @strong{Exercise 3.} If the input data do not form a
- rectangle, you can use @w{@kbd{M-# g}} (@code{calc-grab-region})
- to grab the data the way Emacs normally works with regions---it reads
- left-to-right, top-to-bottom, treating line breaks the same as spaces.
- Use this command to find the geometric mean of the following numbers.
- (The geometric mean is the @var{n}th root of the product of @var{n} numbers.)
- X
- @example
- 2.3 6 22 15.1 7
- X 15 14 7.5
- X 2.5
- @end example
- X
- @noindent
- The @kbd{M-# g} command accepts numbers separated by spaces or commas,
- with or without surrounding vector brackets.
- @xref{List Answer 3, 3}. (@bullet{})
- X
- @ifinfo
- As another example, a theorem about binomial coefficients tells
- us that the alternating sum of binomial coefficients
- @var{n}-choose-0 minus @var{n}-choose-1 plus @var{n}-choose-2, and so
- on up to @var{n}-choose-@var{n},
- always comes out to zero. Let's verify this
- for @cite{n=6}.@refill
- @end ifinfo
- @tex
- As another example, a theorem about binomial coefficients tells
- us that the alternating sum of binomial coefficients
- ${n \choose 0} - {n \choose 1} + {n \choose 2} - \cdots \pm {n \choose n}$
- always comes out to zero. Let's verify this
- for \cite{n=6}.
- @end tex
- X
- @group
- @smallexample
- 1: [1, 2, 3, 4, 5, 6, 7] 1: [0, 1, 2, 3, 4, 5, 6]
- X . .
- X
- X v x 7 RET 1 -
- X
- @end smallexample
- @end group
- @noindent
- @group
- @smallexample
- 1: [1, -6, 15, -20, 15, -6, 1] 1: 0
- X . .
- X
- X V M ' (-1)^$ choose(6,$) RET V R +
- @end smallexample
- @end group
- X
- The @kbd{V M '} command prompts you to enter any algebraic expression
- to define the function to map over the vector. The symbol @samp{$}
- inside this expression represents the argument to the function.
- The Calculator applies this formula to each element of the vector,
- substituting each element's value for the @samp{$} sign(s) in turn.
- X
- To define a two-argument function, use @samp{$$} for the first
- argument and @samp{$} for the second: @kbd{V M ' $$-$ RET} is
- equivalent to @kbd{V M -}. This is analogous to regular algebraic
- entry, where @samp{$$} would refer to the next-to-top stack entry
- and @samp{$} would refer to the top stack entry, and @kbd{' $$-$ RET}
- would act exactly like @kbd{-}.
- X
- Notice that the @kbd{V M '} command has recorded two things in the
- trail: The result, as usual, and also a funny-looking thing marked
- @samp{oper} that represents the operator function you typed in.
- The function is enclosed in @samp{< >} brackets, and the argument is
- denoted by a @samp{#} sign. If there were several arguments, they
- would be shown as @samp{#1}, @samp{#2}, and so on. (For example,
- @kbd{V M ' $$-$} will put the function @samp{<#1 - #2>} on the
- trail.) This object is a ``nameless function''; you can use nameless
- @samp{< >} notation to answer the @kbd{V M '} prompt if you like.
- Nameless function notation has the interesting, occasionally useful
- property that a nameless function is not actually evaluated until
- it is used. For example, @kbd{V M ' $+random(2.0)} evaluates
- @samp{random(2.0)} once and adds that random number to all elements
- of the vector, but @kbd{V M ' <#+random(2.0)>} evaluates the
- @samp{random(2.0)} separately for each vector element.
- X
- Another group of operators that are often useful with @kbd{V M} are
- the relational operators: @kbd{a =}, for example, compares two numbers
- and gives the result 1 if they are equal, or 0 if not. Similarly,
- @w{@kbd{a <}} checks for one number being less than another.
- X
- Other useful vector operations include @kbd{v v}, to reverse a
- vector end-for-end; @kbd{V S}, to sort the elements of a vector
- into increasing order; and @kbd{v r} and @w{@kbd{v c}}, to extract
- one row or column of a matrix, or (in both cases) to extract one
- element of a plain vector. With a negative argument, @kbd{v r}
- and @kbd{v c} instead delete one row, column, or vector element.
- X
- @cindex Divisor functions
- (@bullet{}) @strong{Exercise 4.} The @cite{k}th @dfn{divisor function}
- @tex
- $\sigma_k(n)$
- @end tex
- is the sum of the @cite{k}th powers of all the divisors of an
- integer @cite{n}. Figure out a method for computing the divisor
- function for reasonably small values of @cite{n}. As a test,
- the 0th and 1st divisor functions of 30 are 8 and 72, respectively.
- @xref{List Answer 4, 4}. (@bullet{})
- X
- @cindex Square-free numbers
- @cindex Duplicate values in a list
- (@bullet{}) @strong{Exercise 5.} The @kbd{k f} command produces a
- list of prime factors for a number. Sometimes it is important to
- know that a number is @dfn{square-free}, i.e., that no prime occurs
- more than once in its list of prime factors. Find a sequence of
- keystrokes to tell if a number is square-free; your method should
- leave 1 on the stack if it is, or 0 if it isn't.
- @xref{List Answer 5, 5}. (@bullet{})
- X
- @cindex Triangular lists
- (@bullet{}) @strong{Exercise 6.} Build a list of lists that looks
- like the following diagram. (You may wish to use the @kbd{v /}
- command to enable multi-line display of vectors.)
- X
- @group
- @smallexample
- 1: [ [1],
- X [1, 2],
- X [1, 2, 3],
- X [1, 2, 3, 4],
- X [1, 2, 3, 4, 5],
- X [1, 2, 3, 4, 5, 6] ]
- @end smallexample
- @end group
- X
- @noindent
- @xref{List Answer 6, 6}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 7.} Build the following list of lists.
- X
- @group
- @smallexample
- 1: [ [0],
- X [1, 2],
- X [3, 4, 5],
- X [6, 7, 8, 9],
- X [10, 11, 12, 13, 14],
- X [15, 16, 17, 18, 19, 20] ]
- @end smallexample
- @end group
- X
- @noindent
- @xref{List Answer 7, 7}. (@bullet{})
- X
- @cindex Maximizing a function over a list of values
- @c [fix-ref Numerical Solutions]
- (@bullet{}) @strong{Exercise 8.} Compute a list of values of Bessel's
- @c{$J_1(x)$}
- @cite{J1} function @samp{besJ(1,x)} for @cite{x} from 0 to 5
- in steps of 0.25.
- Find the value of @cite{x} (from among the above set of values) for
- which @samp{besJ(1,x)} is a maximum. Use an ``automatic'' method,
- i.e., just reading along the list by hand to find the largest value
- is not allowed! (There is an @kbd{a X} command which does this kind
- of thing automatically; @pxref{Numerical Solutions}.)
- @xref{List Answer 8, 8}. (@bullet{})@refill
- X
- @cindex Digits, vectors of
- (@bullet{}) @strong{Exercise 9.} You are given an integer in the range
- @c{$0 \le N < 10^m$}
- @cite{0 <= N < 10^m} for @cite{m=12} (i.e., an integer of less than
- twelve digits). Convert this integer into a vector of @cite{m}
- digits, each in the range from 0 to 9. In vector-of-digits notation,
- add one to this integer to produce a vector of @cite{m+1} digits
- (since there could be a carry out of the most significant digit).
- Convert this vector back into a regular integer. A good integer
- to try is 25129925999. @xref{List Answer 9, 9}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 10.} Your friend Joe tried to use
- @kbd{V R a =} to test if all numbers in a list were equal. What
- happened? How would you do this test? @xref{List Answer 10, 10}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 11.} The area of a circle of radius one
- is @c{$\pi$}
- @cite{pi}. The area of the @c{$2\times2$}
- @asis{2x2} square that encloses that
- circle is 4. So if we throw @i{N} darts at random points in the square,
- about @c{$\pi/4$}
- @cite{pi/4} of them will land inside the circle. This gives us
- an entertaining way to estimate the value of @c{$\pi$}
- @cite{pi}. The @w{@kbd{k r}}
- command picks a random number between zero and the value on the stack.
- We could get a random floating-point number between @i{-1} and 1 by typing
- @w{@kbd{2.0 k r 1 -}}. Build a vector of 100 random @cite{(x,y)} points in
- this square, then use vector mapping and reduction to count how many
- points lie inside the unit circle. Hint: Use the @kbd{v b} command.
- @xref{List Answer 11, 11}. (@bullet{})
- X
- @cindex Matchstick problem
- (@bullet{}) @strong{Exercise 12.} The @dfn{matchstick problem} provides
- another way to calculate @c{$\pi$}
- @cite{pi}. Say you have an infinite field
- of vertical lines with a spacing of one inch. Toss a one-inch matchstick
- onto the field. The probability that the matchstick will land crossing
- a line turns out to be @c{$2/\pi$}
- @cite{2/pi}. Toss 100 matchsticks to estimate
- @c{$\pi$}
- @cite{pi}. (If you want still more fun, the probability that the GCD
- (@w{@kbd{k g}}) of two large integers is one turns out to be @c{$6/\pi^2$}
- @cite{6/pi^2}.
- That provides yet another way to estimate @c{$\pi$}
- @cite{pi}.)
- @xref{List Answer 12, 12}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 13.} An algebraic entry of a string in
- double-quote marks, @samp{"hello"}, creates a vector of the numerical
- (ASCII) codes of the characters (here, @cite{[104, 101, 108, 108, 111]}).
- Sometimes it is convenient to compute a @dfn{hash code} of a string,
- which is just an integer that represents the value of that string.
- Two equal strings have the same hash code; two different strings
- @dfn{probably} have different hash codes. (For example, Calc has
- over 400 function names, but Emacs can quickly find the definition for
- any given name because it has sorted the functions into ``buckets'' by
- their hash codes. Sometimes a few names will hash into the same bucket,
- but it is easier to search among a few names than among all the names.)
- One popular hash function is computed as follows: First set @cite{h = 0}.
- Then, for each character from the string in turn, set @cite{h = 3h + c_i}
- where @cite{c_i} is the character's ASCII code. If we have 511 buckets,
- we then take the hash code modulo 511 to get the bucket number. Develop a
- simple command or commands for converting string vectors into hash codes.
- The hash code for @samp{"Testing, 1, 2, 3"} is 1960915098, which modulo
- 511 is 121. @xref{List Answer 13, 13}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 14.} The @kbd{H V R} and @kbd{H V U}
- commands do nested function evaluations. @kbd{H V U} takes a starting
- value and a number of steps @var{n} from the stack; it then applies the
- function you give to the starting value 0, 1, 2, up to @var{n} times
- and returns a vector of the results. Use this command to create a
- ``random walk'' of 50 steps. Start with the two-dimensional point
- @cite{(0,0)}; then take one step a random distance between @i{-1} and 1
- in both @cite{x} and @cite{y}; then take another step, and so on. Use the
- @kbd{g f} command to display this random walk. Now modify your random
- walk to walk a unit distance, but in a random direction, at each step.
- (Hint: The @code{sincos} function returns a vector of the cosine and
- sine of an angle.) @xref{List Answer 14, 14}. (@bullet{})
- X
- @node Types Tutorial, Algebra Tutorial, Vector/Matrix Tutorial, Tutorial
- @section Types Tutorial
- X
- @noindent
- Calc understands a variety of data types as well as simple numbers.
- In this section, we'll experiment with each of these types in turn.
- X
- The numbers we've been using so far have mainly been either @dfn{integers}
- or @dfn{floats}. We saw that floats are usually a good approximation to
- the mathematical concept of real numbers, but they are only approximations
- and are susceptible to roundoff error. Calc also supports @dfn{fractions},
- which can exactly represent any rational number.
- X
- @group
- @smallexample
- 1: 3628800 2: 3628800 1: 518400:7 1: 518414:7 1: 7:518414
- X . 1: 49 . . .
- X .
- X
- X 10 ! 49 RET : 2 + &
- @end smallexample
- @end group
- X
- @noindent
- The @kbd{:} command divides two integers to get a fraction; @kbd{/}
- would normally divide integers to get a floating-point result.
- Notice we had to type @key{RET} between the @kbd{49} and the @kbd{:}
- since the @kbd{:} would otherwise be interpreted as part of a
- fraction beginning with 49.
- X
- You can convert between floating-point and fractional format using
- @kbd{c f} and @kbd{c F}:
- X
- @group
- @smallexample
- 1: 1.35027217629e-5 1: 7:518414
- X . .
- X
- X c f c F
- @end smallexample
- @end group
- X
- The @kbd{c F} command replaces a floating-point number with the
- ``simplest'' fraction whose floating-point representation is the
- same, to within the current precision.
- X
- @group
- @smallexample
- 1: 3.14159265359 1: 1146408:364913 1: 3.1416 1: 355:113
- X . . . .
- X
- X P c F DEL p 5 RET P c F
- @end smallexample
- @end group
- X
- (@bullet{}) @strong{Exercise 1.} A calculation has produced the
- result 1.26508260337. You suspect it is the square root of the
- product of @c{$\pi$}
- @cite{pi} and some rational number. Is it? (Be sure
- to allow for roundoff error!) @xref{Types Answer 1, 1}. (@bullet{})
- X
- @dfn{Complex numbers} can be stored in both rectangular and polar form.
- X
- @group
- @smallexample
- 1: -9 1: (0, 3) 1: (3; 90.) 1: (6; 90.) 1: (2.4495; 45.)
- X . . . . .
- X
- X 9 n Q c p 2 * Q
- @end smallexample
- @end group
- X
- @noindent
- The square root of @i{-9} is by default rendered in rectangular form
- (@w{@cite{0 + 3i}}), but we can convert it to polar form (3 with a
- phase angle of 90 degrees). All the usual arithmetic and scientific
- operations are defined on both types of complex numbers.
- X
- Another generalized kind of number is @dfn{infinity}. Infinity
- isn't really a number, but it can sometimes be treated like one.
- Calc uses the symbol @code{inf} to represent positive infinity,
- i.e., a value greater than any real number. Naturally, you can
- also write @samp{-inf} for minus infinity, a value less than any
- real number. The word @code{inf} can only be input using
- algebraic entry.
- X
- @group
- @smallexample
- 2: inf 2: -inf 2: -inf 2: -inf 1: nan
- 1: -17 1: -inf 1: -inf 1: inf .
- X . . . .
- X
- ' inf RET 17 n * RET 72 + A +
- @end smallexample
- @end group
- X
- @noindent
- Since infinity is infinitely large, multiplying it by any finite
- number (like @i{-17}) has no effect, except that since @i{-17}
- is negative, it changes a plus infinity to a minus infinity.
- (``A huge positive number, multiplied by @i{-17}, yields a huge
- negative number.'') Adding any finite number to infinity also
- leaves it unchanged. Taking an absolute value gives us plus
- infinity again. Finally, we add this plus infinity to the minus
- infinity we had earlier. If you work it out, you might expect
- the answer to be @i{-72} for this. But the 72 has been completely
- lost next to the infinities; by the time we compute @w{@samp{inf - inf}}
- the finite difference between them, if any, is indetectable.
- So we say the result is @dfn{indeterminate}, which Calc writes
- with the symbol @code{nan} (for Not A Number).
- X
- Dividing by zero is normally treated as an error, but you can get
- Calc to write an answer in terms of infinity by pressing @kbd{m i}
- to turn on ``infinite mode.''
- X
- @group
- @smallexample
- 3: nan 2: nan 2: nan 2: nan 1: nan
- 2: 1 1: 1 / 0 1: uinf 1: uinf .
- 1: 0 . . .
- X .
- X
- X 1 RET 0 / m i U / 17 n * +
- @end smallexample
- @end group
- X
- @noindent
- Dividing by zero normally is left unevaluated, but after @kbd{m i}
- it instead gives an infinite result. The answer is actually
- @code{uinf}, ``undirected infinity.'' If you look at a graph of
- @cite{1 / x} around @w{@cite{x = 0}}, you'll see that it goes toward
- plus infinity as you approach zero from above, but toward minus
- infinity as you approach from below. Since we said only @cite{1 / 0},
- Calc knows that the answer is infinite but not in which direction.
- That's what @code{uinf} means. Notice that multiplying @code{uinf}
- by a negative number still leaves plain @code{uinf}; there's no
- point in saying @samp{-uinf} because the sign of @code{uinf} is
- unknown anyway. Finally, we add @code{uinf} to our @code{nan},
- yielding @code{nan} again. It's easy to see that, because
- @code{nan} means ``totally unknown'' while @code{uinf} means
- ``unknown sign but known to be infinite,'' the more mysterious
- @code{nan} wins out when it is combined with @code{uinf}, or, for
- that matter, with anything else.
- X
- (@bullet{}) @strong{Exercise 2.} Predict what Calc will answer
- for each of these formulas: @samp{inf / inf}, @samp{exp(inf)},
- @samp{exp(-inf)}, @samp{sqrt(-inf)}, @samp{sqrt(uinf)},
- @samp{abs(uinf)}, @samp{ln(0)}.
- @xref{Types Answer 2, 2}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 3.} We saw that @samp{inf - inf = nan},
- which stands for an unknown value. Can @code{nan} stand for
- a complex number? Can it stand for infinity?
- @xref{Types Answer 3, 3}. (@bullet{})
- X
- @dfn{HMS forms} represent a value in terms of hours, minutes, and
- seconds.
- X
- @group
- @smallexample
- 1: 2@@ 30' 0" 1: 3@@ 30' 0" 2: 3@@ 30' 0" 1: 2.
- X . . 1: 1@@ 45' 0." .
- X .
- X
- X 2@@ 30' RET 1 + RET 2 / /
- @end smallexample
- @end group
- X
- HMS forms can also be used to hold angles in degrees, minutes, and
- seconds.
- X
- @group
- @smallexample
- 1: 0.5 1: 26.56505 1: 26@@ 33' 54.18" 1: 0.44721
- X . . . .
- X
- X 0.5 I T c h S
- @end smallexample
- @end group
- X
- @noindent
- First we convert the inverse tangent of 0.5 to degrees-minutes-seconds
- form, then we take the sine of that angle. Note that the trigonometric
- functions will accept HMS forms directly as input.
- X
- @cindex Beatles
- (@bullet{}) @strong{Exercise 4.} The Beatles' @emph{Abbey Road} is
- 47 minutes and 26 seconds long, and contains 17 songs. What is the
- average length of a song on @emph{Abbey Road}? If the Extended Disco
- Version of @emph{Abbey Road} added 20 seconds to the length of each
- song, how long would the album be? @xref{Types Answer 4, 4}. (@bullet{})
- X
- A @dfn{date form} represents a date, or a date and time. Dates must
- be entered using algebraic entry. Date forms are surrounded by
- @samp{< >} symbols; most standard formats for dates are recognized.
- X
- @group
- @smallexample
- 2: <Sun Jan 13, 1991> 1: 2.25
- 1: <6:00pm Thu Jan 10, 1991> .
- X .
- X
- ' <13 Jan 1991>, <1/10/91, 6pm> RET -
- @end smallexample
- @end group
- X
- @noindent
- In this example, we enter two dates, then subtract to find the
- number of days between them. It is also possible to add an
- HMS form or a number (of days) to a date form to get another
- date form.
- X
- @group
- @smallexample
- 1: <4:45:59pm Mon Jan 14, 1991> 1: <2:50:59am Thu Jan 17, 1991>
- X . .
- X
- X t N 2 + 10@@ 5' +
- @end smallexample
- @end group
- X
- @c [fix-ref Date Arithmetic]
- @noindent
- The @kbd{t N} (``now'') command pushes the current date and time on the
- stack; then we add two days, ten hours and five minutes to the date and
- time. Other date-and-time related commands include @kbd{t J}, which
- does Julian day conversions, @kbd{t W}, which finds the beginning of
- the week in which a date form lies, and @kbd{t I}, which increments a
- date by one or several months. @xref{Date Arithmetic}, for more.
- X
- (@bullet{}) @strong{Exercise 5.} How many days until the next
- Friday the 13th? @xref{Types Answer 5, 5}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 6.} How many leap years will there be
- between now and the year 10001 A.D.? @xref{Types Answer 6, 6}. (@bullet{})
- X
- @cindex Slope and angle of a line
- @cindex Angle and slope of a line
- An @dfn{error form} represents a mean value with an attached standard
- deviation, or error estimate. Suppose our measurements indicate that
- a certain telephone pole is about 30 meters away, with an estimated
- error of 1 meter, and 8 meters tall, with an estimated error of 0.2
- meters. What is the slope of a line from here to the top of the
- pole, and what is the equivalent angle in degrees?
- X
- @group
- @smallexample
- 1: 8 +/- 0.2 2: 8 +/- 0.2 1: 0.266 +/- 0.011 1: 14.93 +/- 0.594
- X . 1: 30 +/- 1 . .
- X .
- X
- X 8 p .2 RET 30 p 1 / I T
- @end smallexample
- @end group
- X
- @noindent
- This means that the angle is about 15 degrees, and, assuming our
- original error estimates were valid standard deviations, there is about
- a 60% chance that the result is correct within 0.59 degrees.
- X
- @cindex Torus, volume of
- (@bullet{}) @strong{Exercise 7.} The volume of a torus (a donut shape) is
- @c{$2 \pi^2 R r^2$}
- @w{@cite{2 pi^2 R r^2}} where @cite{R} is the radius of the circle that
- defines the center of the tube and @cite{r} is the radius of the tube
- itself. Suppose @cite{R} is 20 cm and @cite{r} is 4 cm, each known to
- within 5 percent. What is the volume and the relative uncertainty of
- the volume? @xref{Types Answer 7, 7}. (@bullet{})
- X
- An @dfn{interval form} represents a range of values. While an
- error form is best for making statistical estimates, intervals give
- you exact bounds on an answer. Suppose we additionally know that
- our telephone pole is definitely between 28 and 31 meters away,
- and that it is between 7.7 and 8.1 meters tall.
- X
- @group
- @smallexample
- 1: [7.7 .. 8.1] 2: [7.7 .. 8.1] 1: [0.24 .. 0.28] 1: [13.9 .. 16.1]
- X . 1: [28 .. 31] . .
- X .
- X
- X [ 7.7 .. 8.1 ] [ 28 .. 31 ] / I T
- @end smallexample
- @end group
- X
- @noindent
- If our bounds were correct, then the angle to the top of the pole
- is sure to lie in the range shown.
- X
- The square brackets around these intervals indicate that the endpoints
- themselves are allowable values. In other words, the distance to the
- telephone pole is between 28 and 31, @emph{inclusive}. You can also
- make an interval that is exclusive of its endpoints by writing
- parentheses instead of square brackets. You can even make an interval
- which is inclusive (``closed'') on one end and exclusive (``open'') on
- the other.
- X
- @group
- @smallexample
- 1: [1 .. 10) 1: (0.1 .. 1] 2: (0.1 .. 1] 1: (0.2 .. 3)
- X . . 1: [2 .. 3) .
- X .
- X
- X [ 1 .. 10 ) & [ 2 .. 3 ) *
- @end smallexample
- @end group
- X
- @noindent
- The Calculator automatically keeps track of which end values should
- be open and which should be closed. You can also make infinite or
- semi-infinite intervals by using @samp{-inf} or @samp{inf} for one
- or both endpoints.
- X
- (@bullet{}) @strong{Exercise 8.} What answer would you expect from
- @samp{@w{1 /} @w{(0 .. 10)}}? What about @samp{@w{1 /} @w{(-10 .. 0)}}? What
- about @samp{@w{1 /} @w{[0 .. 10]}} (where the interval actually includes
- zero)? What about @samp{@w{1 /} @w{(-10 .. 10)}}?
- @xref{Types Answer 8, 8}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 9.} Two easy ways of squaring a number
- are @kbd{RET *} and @w{@kbd{2 ^}}. Normally these produce the same
- answer. Would you expect this still to hold true for interval forms?
- If not, which of these will result in a larger interval?
- @xref{Types Answer 9, 9}. (@bullet{})
- X
- A @dfn{modulo form} is used for performing arithmetic modulo @i{M}.
- For example, arithmetic involving time is generally done modulo 12
- or 24 hours.
- X
- @group
- @smallexample
- 1: 17 mod 24 1: 3 mod 24 1: 21 mod 24 1: 9 mod 24
- X . . . .
- X
- X 17 M 24 RET 10 + n 5 /
- @end smallexample
- @end group
- X
- @noindent
- In this last step, Calc has found a new number which, when multiplied
- by 5 modulo 24, produces the original number, 21. If @i{M} is prime
- it is always possible to find such a number. For non-prime @i{M}
- like 24, it is only sometimes possible.
- X
- @group
- @smallexample
- 1: 10 mod 24 1: 16 mod 24 1: 1000000... 1: 16
- X . . . .
- X
- X 10 M 24 RET 100 ^ 10 RET 100 ^ 24 %
- @end smallexample
- @end group
- X
- @noindent
- These two calculations get the same answer, but the first one is
- much more efficient because it avoids the huge intermediate value
- that arises in the second one.
- X
- @cindex Fermat, primality test of
- (@bullet{}) @strong{Exercise 10.} A theorem of Pierre de Fermat
- says that @c{\w{$x^{n-1} \bmod n = 1$}}
- @cite{x^(n-1) mod n = 1} if @cite{n} is a prime number
- and @cite{x} is an integer less than @cite{n}. If @cite{n} is
- @emph{not} a prime number, this will @emph{not} be true for most
- values of @cite{x}. Thus we can test informally if a number is
- prime by trying this formula for several values of @cite{x}.
- Use this test to tell whether the following numbers are prime:
- 811749613, 15485863. @xref{Types Answer 10, 10}. (@bullet{})
- X
- It is possible to use HMS forms as parts of error forms, intervals,
- modulo forms, or as the phase part of a polar complex number.
- For example, the @code{calc-time} command pushes the current time
- of day on the stack as an HMS/modulo form.
- X
- @group
- @smallexample
- 1: 17@@ 34' 45" mod 24@@ 0' 0" 1: 6@@ 22' 15" mod 24@@ 0' 0"
- X . .
- X
- X x time RET n
- @end smallexample
- @end group
- X
- @noindent
- This calculation tells me it is six hours and 22 minutes until midnight.
- X
- (@bullet{}) @strong{Exercise 11.} A rule of thumb is that one year
- is about @c{$\pi \times 10^7$}
- @w{@cite{pi * 10^7}} seconds. What time will it be that
- many seconds from right now? @xref{Types Answer 11, 11}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 12.} You are preparing to order packaging
- for the CD release of the Extended Disco Version of @emph{Abbey Road}.
- You are told that the songs will actually be anywhere from 20 to 60
- seconds longer than the originals. One CD can hold about 75 minutes
- of music. Should you order single or double packages?
- @xref{Types Answer 12, 12}. (@bullet{})
- X
- Another kind of data the Calculator can manipulate is numbers with
- @dfn{units}. This isn't strictly a new data type; it's simply an
- application of algebraic expressions, where we use variables with
- suggestive names like @samp{cm} and @samp{in} to represent units
- like centimeters and inches.
- X
- @group
- @smallexample
- 1: 2 in 1: 5.08 cm 1: 0.027778 fath 1: 0.0508 m
- X . . . .
- X
- X ' 2in RET u c cm RET u c fath RET u b
- @end smallexample
- @end group
- X
- @noindent
- We enter the quantity ``2 inches'' (actually an algebraic expression
- which means two times the variable @samp{in}), then we convert it
- first to centimeters, then to fathoms, then finally to ``base'' units,
- which in this case means meters.
- X
- @group
- @smallexample
- 1: 9 acre 1: 3 sqrt(acre) 1: 190.84 m 1: 190.84 m + 30 cm
- X . . . .
- X
- X ' 9 acre RET Q u s ' $+30 cm RET
- X
- @end smallexample
- @end group
- @noindent
- @group
- @smallexample
- 1: 191.14 m 1: 36536.3046 m^2 1: 365363046 cm^2
- X . . .
- X
- X u s 2 ^ u c cgs
- @end smallexample
- @end group
- X
- @noindent
- Since units expressions are really just formulas, taking the square
- root of @samp{acre} is undefined. After all, @code{acre} might be an
- algebraic variable that you will someday assign a value. We use the
- ``units-simplify'' command to simplify the expression with variables
- being interpreted as unit names.
- X
- In the final step, we have converted not to a particular unit, but to a
- units system. The ``cgs'' system uses centimeters instead of meters
- as its standard unit of length.
- X
- There are a wide variety of units defined in the Calculator.
- X
- @group
- @smallexample
- 1: 55 mph 1: 88.5139 kph 1: 88.5139 km / hr 1: 8.201407e-8 c
- X . . . .
- X
- X ' 55 mph RET u c kph RET u c km/hr RET u c c RET
- @end smallexample
- @end group
- X
- @noindent
- We express a speed first in miles per hour, then in kilometers per
- hour, then again using a slightly more explicit notation, then
- finally in terms of fractions of the speed of light.
- X
- Temperature conversions are a bit more tricky. There are two ways to
- interpret ``20 degrees Fahrenheit''---it could mean an actual
- temperature, or it could mean a change in temperature. For normal
- units there is no difference, but temperature units have an offset
- as well as a scale factor and so there must be two explicit commands
- for them.
- X
- @group
- @smallexample
- 1: 20 degF 1: 11.1111 degC 1: -20:3 degC 1: -6.666 degC
- X . . . .
- X
- X ' 20 degF RET u c degC RET U u t degC RET c f
- @end smallexample
- @end group
- X
- @noindent
- First we convert a change of 20 degrees Fahrenheit into an equivalent
- change in degrees Celsius (or Centigrade). Then, we convert the
- absolute temperature 20 degrees Fahrenheit into Celsius. Since
- this comes out as an exact fraction, we then convert to floating-point
- for easier comparison with the other result.
- X
- For simple unit conversions, you can put a plain number on the stack.
- Then @kbd{u c} and @kbd{u t} will prompt for both old and new units.
- When you use this method, you're responsible for remembering which
- numbers are in which units:
- X
- @group
- @smallexample
- 1: 55 1: 88.5139 1: 8.201407e-8
- X . . .
- X
- X 55 u c mph RET kph RET u c km/hr RET c RET
- @end smallexample
- @end group
- X
- To see a complete list of built-in units, type @kbd{u v}. Press
- @w{@kbd{M-# c}} again to re-enter the Calculator when you're done looking
- at the units table.
- X
- (@bullet{}) @strong{Exercise 13.} How many seconds are there really
- in a year? @xref{Types Answer 13, 13}. (@bullet{})
- X
- @cindex Speed of light
- (@bullet{}) @strong{Exercise 14.} Supercomputer designs are limited by
- the speed of light (and of electricity, which is nearly as fast).
- Suppose a computer has a 4.1 ns (nanosecond) clock cycle, and its
- cabinet is one meter across. Is speed of light going to be a
- significant factor in its design? @xref{Types Answer 14, 14}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 15.} Sam the Slug normally travels about
- five yards in an hour. He has obtained a supply of Power Pills; each
- Power Pill he eats doubles his speed. How many Power Pills can he
- swallow and still travel legally on most US highways?
- @xref{Types Answer 15, 15}. (@bullet{})
- X
- @node Algebra Tutorial, Programming Tutorial, Types Tutorial, Tutorial
- @section Algebra and Calculus Tutorial
- X
- @noindent
- This section shows how to use Calc's algebra facilities to solve
- equations, do simple calculus problems, and manipulate algebraic
- formulas.
- X
- @menu
- * Basic Algebra Tutorial::
- * Rewrites Tutorial::
- @end menu
- X
- @node Basic Algebra Tutorial, Rewrites Tutorial, Algebra Tutorial, Algebra Tutorial
- @subsection Basic Algebra
- X
- @noindent
- If you enter a formula in algebraic mode that refers to variables,
- the formula itself is pushed onto the stack. You can manipulate
- formulas as regular data objects.
- X
- @group
- @smallexample
- 1: 2 x^2 - 6 1: 6 - 2 x^2 1: (6 - 2 x^2) (3 x^2 + y)
- X . . .
- X
- X ' 2x^2-6 RET n ' 3x^2+y RET *
- @end smallexample
- @end group
- X
- (@bullet{}) @strong{Exercise 1.} Do @kbd{' x RET Q 2 ^} and
- @kbd{' x RET 2 ^ Q} both wind up with the same result (@samp{x})?
- Why or why not? @xref{Algebra Answer 1, 1}. (@bullet{})
- X
- There are also commands for doing common algebraic operations on
- formulas. Continuing with the formula from the last example,
- X
- @group
- @smallexample
- 1: 18 x^2 + 6 y - 6 x^4 - 2 x^2 y 1: (18 - 2 y) x^2 - 6 x^4 + 6 y
- X . .
- X
- X a x a c x RET
- @end smallexample
- @end group
- X
- @noindent
- First we ``expand'' using the distributive law, then we ``collect''
- terms involving like powers of @cite{x}.
- X
- Let's find the value of this expression when @cite{x} is 2 and @cite{y}
- is one-half.
- X
- @group
- @smallexample
- 1: 17 x^2 - 6 x^4 + 3 1: -25
- X . .
- X
- X 1:2 s l y RET 2 s l x RET
- @end smallexample
- @end group
- X
- @noindent
- The @kbd{s l} command means ``let''; it takes a number from the top of
- the stack and temporarily assigns it as the value of the variable
- you specify. It then evaluates (as if by the @kbd{=} key) the
- next expression on the stack. After this command, the variable goes
- back to its original value, if any.
- X
- (An earlier exercise in this tutorial involved storing a value in the
- variable @code{x}; if this value is still there, you will have to
- unstore it with @kbd{s u x RET} before the above example will work
- properly.)
- X
- @cindex Maximum of a function using Calculus
- Let's find the maximum value of our original expression when @cite{y}
- is one-half and @cite{x} ranges over all possible values. We can
- do this by taking the derivative with respect to @cite{x} and examining
- values of @cite{x} for which the derivative is zero. If the second
- derivative of the function at that value of @cite{x} is negative,
- the function has a local maximum there.
- X
- @group
- @smallexample
- 1: 17 x^2 - 6 x^4 + 3 1: 34 x - 24 x^3
- X . .
- X
- X U DEL s 1 a d x RET s 2
- @end smallexample
- @end group
- X
- @noindent
- Well, the derivative is clearly zero when @cite{x} is zero. To find
- the other root(s), let's divide through by @cite{x} and then solve:
- X
- @group
- @smallexample
- 1: (34 x - 24 x^3) / x 1: 34 x / x - 24 x^3 / x 1: 34 - 24 x^2
- X . . .
- X
- X ' x RET / a x a s
- X
- @end smallexample
- @end group
- @noindent
- @group
- @smallexample
- 1: 34 - 24 x^2 = 0 1: x = 1.19023
- X . .
- X
- X 0 a = s 3 a S x RET
- @end smallexample
- @end group
- X
- @noindent
- Notice the use of @kbd{a s} to ``simplify'' the formula. When the
- default algebraic simplifications don't do enough, you can use
- @kbd{a s} to tell Calc to spend more time on the job.
- X
- Now we compute the second derivative and plug in our values of @cite{x}:
- X
- @group
- @smallexample
- 1: 1.19023 2: 1.19023 2: 1.19023
- X . 1: 34 x - 24 x^3 1: 34 - 72 x^2
- X . .
- X
- X a . r 2 a d x RET s 4
- @end smallexample
- @end group
- X
- @noindent
- (The @kbd{a .} command extracts just the righthand side of an equation.
- Another method would have been to use @kbd{v u} to unpack the equation
- @w{@samp{x = 1.19}} to @samp{x} and @samp{1.19}, then use @kbd{M-- M-2 DEL}
- to delete the @samp{x}.)
- X
- @group
- @smallexample
- 2: 34 - 72 x^2 1: -68. 2: 34 - 72 x^2 1: 34
- 1: 1.19023 . 1: 0 .
- X . .
- X
- X TAB s l x RET U DEL 0 s l x RET
- @end smallexample
- @end group
- X
- @noindent
- The first of these second derivatives is negative, so we know the function
- SHAR_EOF
- true || echo 'restore of calc.texinfo failed'
- fi
- echo 'End of part 33'
- echo 'File calc.texinfo is continued in part 34'
- echo 34 > _shar_seq_.tmp
- exit 0
- exit 0 # Just in case...
- --
- Kent Landfield INTERNET: kent@sparky.IMD.Sterling.COM
- Sterling Software, IMD UUCP: uunet!sparky!kent
- Phone: (402) 291-8300 FAX: (402) 291-4362
- Please send comp.sources.misc-related mail to kent@uunet.uu.net.
-